<?php

/**
 * Flickr Photosets - Invite Page
 * Helps the user to invite their friends who haven't added the application.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 2.0
 * @date 9/20/07
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since File available since Release 2.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets
*/
$fp = new FlickrPhotosetsCanvas(FlickrPhotosetsCanvas::require_add, true, false);

if (isset($_GET['all'])) {
	
	// Get list of friends who have this app installed
	$fql_result = $fp->facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = $fp->uid)");
	$friends_to_exclude = "";

	// Build a delimited list of users to exclude
	for ($i = 0; $i < count($fql_result); $i++) {
		if ($friends_to_exclude != "") {
			$friends_to_exclude .= ",";
		}
		$friends_to_exclude .= $fql_result[$i]["uid"];
	}
	
	$about_text = 'Using this page you can invite any of your friends to use '.$fp->app_name.'. If you want to see only your Facebook friends that are also in your contacts on Flickr, <a href="'.$fp->app_url.'/invite.php">show the invite box with only your Flickr contacts</a>.';
	
}
else {
	
	// Get the Flickr contacts and Facebook friends
	$fp->flickr->setToken($fp->config['flickr_token']);
	$flickr_contacts = $fp->flickr->contacts_getList();
	$facebook_friend_uids = $fp->facebook->api_client->friends_get();
	$facebook_friends_added = $fp->facebook->api_client->friends_getAppUsers();
		
	// Get the name of your Flickr contacts
	$flickr_friend_names = array();
	foreach ($flickr_contacts['contact'] as $friend) {
		if (!empty($friend['realname'])) {
			array_push($flickr_friend_names, $friend['realname']);
		}
	}
		
	// Get the names of your Facebook friends
	$facebook_friend_names = $fp->facebook->api_client->users_getInfo($facebook_friend_uids, array('name'));
	
	/*
	Do our best to match the names.
	If there is a match, remove the user from the list because we are building
	a list of users to exclude.
	*/
	foreach ($flickr_friend_names as $friend) {
		$friend_key = $fp->multi_array_search($friend, $facebook_friend_names);
		if (!empty($friend_key)) {
			unset($facebook_friend_names[$friend_key[0]]);
		}
	}
	
	// Add friends to the list who have added the app
	foreach ($facebook_friends_added as $friend) {
		array_push($facebook_friend_names, array('uid'=>$friend));
	}
	
	// What is left should be the ones to exclude
	$friends_to_exclude = "";
	foreach ($facebook_friend_names as $friend) {
		if ($friends_to_exclude != "") {
			$friends_to_exclude .= ",";
		}
		$friends_to_exclude .= $friend['uid'];
	}
	
	$about_text = 'We\'ve done our best to find your Facebook friends that are also in your Flickr contacts, but have not yet added '.$fp->app_name.'. If you know that one of your Facebook friends has a Flickr account, but they are not in the list below, <a href="'.$fp->app_url.'/invite.php?all">show the invite box with all of your friends</a>.';
	
}

// The invite box text
$inv_fbml = '<fb:name uid="'.$fp->uid.'" firstnameonly="true" shownetwork="false"/> wants you to share your Flickr photos on Facebook.<fb:req-choice url="http://www.facebook.com/add.php?api_key='.FACEBOOK_API_KEY.'" label="Add '.$fp->app_name.'" />';

?>

<?=$fp->print_dashboard()?>

<div class="answer" style="margin-left:20px;margin-right:20px;line-height: 16px;">
	<?=$about_text?>
</div>

<fb:request-form type="invitation" content="<?=htmlentities($inv_fbml)?>" action="index.php">
	<fb:multi-friend-selector actiontext="Invite your friends to use <?=$fp->app_name?>." max="10" exclude_ids="<?=$friends_to_exclude?>" />
</fb:request-form>

<?=$fp->echo_footer(true)?>